package com.snj.visangedu;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.directionalviewpager.DirectionalViewPager;
import com.snj.visangedu.lib.CustomHttpClient;
import com.snj.visangedu.lib.Global;


public class ContentActivity3 extends FragmentActivity {
	
	String tag = "ContentActivity3";
	int viewpagercurrentpos = 1;
	int currentposition = 0;
	
	String no = "";
	String prev = "", next = "";
	
	String type = "1";
	
	ImageView img_next = null;
	ImageView img_prev = null;
	ImageView img_close = null;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		
		super.onCreate(savedInstanceState);
		
		Intent contentintent = getIntent();
		
		type = contentintent.getStringExtra("type");
		if(type == null) type = "1";
		
		no = contentintent.getStringExtra("no");
		prev = contentintent.getStringExtra("prev");
		next = contentintent.getStringExtra("next");
		
		currentposition = contentintent.getIntExtra("position", 0);
		
		setContentView(R.layout.activity_content);
		
		img_next = (ImageView)findViewById(R.id.img_nextnews);
		img_prev = (ImageView)findViewById(R.id.img_prevnews);
		img_close = (ImageView)findViewById(R.id.img_closenews);
		
		
		
		final TestFragmentAdapter3 adapter = new TestFragmentAdapter3(getSupportFragmentManager());
		adapter.SetDataset(prev, no, next, currentposition - 1, currentposition, currentposition + 1);
		
		 final DirectionalViewPager pager = (DirectionalViewPager)findViewById(R.id.pager);
	        pager.setAdapter(adapter);
	        pager.setOrientation(DirectionalViewPager.VERTICAL);
	        
	        pager.setCurrentItem(viewpagercurrentpos);
	        
	        	        
	        new AsyncTask<String, String, String>() {

				@Override
				protected String doInBackground(String... params) {
					// TODO Auto-generated method stub
					
					String result = "";
	            	JSONObject resultobj = null;
	            	String imgsrc = "";
	            	Bitmap img = null;
	            	
	            	
	            	
	            	ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
					
	            	
					postParameters.clear();
					postParameters.add(new BasicNameValuePair("no", no));
						
					Log.d(tag, "param2 : "+ postParameters.toString());
					Log.d(tag, "type : " + type);
						
					try {
						result = CustomHttpClient.executeHttpPost(Global.contenturl2, postParameters);							
						resultobj = new JSONObject(result);
							
						Log.d(tag, result);
					}
					catch(Exception e)
					{
						
					}
					return null;
				}

				@Override
				protected void onPostExecute(String result) {
					// TODO Auto-generated method stub
					super.onPostExecute(result);
					
					// ù ��°(���) �����׸�Ʈ �߰�
			        
				}
	        	
			};//.execute("");
	   
	        
			img_next.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent contentintent = new Intent(getApplicationContext(), ContentActivity3.class);
					
					
					currentposition++;
					
					if(currentposition > 0)
						contentintent.putExtra("prev", Global.mNews3.get(currentposition-1).GetNo());
					
					contentintent.putExtra("no", Global.mNews3.get(currentposition).GetNo());
					contentintent.putExtra("position", currentposition);
					if(currentposition < (Global.mNews3.size() - 1))
						contentintent.putExtra("next", Global.mNews3.get(currentposition+1).GetNo());
					startActivity(contentintent);
					ContentActivity3.this.overridePendingTransition(0, 0);
					
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					finish();
				}
			});
			
			img_prev.setOnClickListener(new OnClickListener() {
						
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(currentposition > 0)
					{
						Intent contentintent = new Intent(getApplicationContext(), ContentActivity3.class);
						
						
						currentposition--;
						
						if(currentposition > 0)
							contentintent.putExtra("prev", Global.mNews3.get(currentposition-1).GetNo());
						
						contentintent.putExtra("no", Global.mNews3.get(currentposition).GetNo());
						contentintent.putExtra("position", currentposition);
						if(currentposition < (Global.mNews3.size() - 1))
							contentintent.putExtra("next", Global.mNews3.get(currentposition+1).GetNo());
						startActivity(contentintent);
						ContentActivity3.this.overridePendingTransition(0, 0);
						
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						finish();
					}
				}
			});
			
			img_close.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					finish();
				}
			});
	        
	        pager.setOnPageChangeListener(new OnPageChangeListener() {
				
				@Override
				public void onPageSelected(int position) {
					// TODO Auto-generated method stub
					
					ContentFragment3 frg = (ContentFragment3)adapter.getItem(position);
					//Log.d(tag, frg.mContent);
					
					if(position == 0 && currentposition > 0)
					{
						Intent contentintent = new Intent(getApplicationContext(), ContentActivity3.class);
						
						
						currentposition--;
						
						if(currentposition > 0)
							contentintent.putExtra("prev", Global.mNews3.get(currentposition-1).GetNo());
						
						contentintent.putExtra("no", Global.mNews3.get(currentposition).GetNo());
						contentintent.putExtra("position", currentposition);
						if(currentposition < (Global.mNews3.size() - 1))
							contentintent.putExtra("next", Global.mNews3.get(currentposition+1).GetNo());
						startActivity(contentintent);
						ContentActivity3.this.overridePendingTransition(0, 0);
						
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						finish();
					}
					else if(position == 2)
					{						
						
						Intent contentintent = new Intent(getApplicationContext(), ContentActivity3.class);
						
						
						currentposition++;
						
						if(currentposition > 0)
							contentintent.putExtra("prev", Global.mNews3.get(currentposition-1).GetNo());
						
						contentintent.putExtra("no", Global.mNews3.get(currentposition).GetNo());
						contentintent.putExtra("position", currentposition);
						if(currentposition < (Global.mNews3.size() - 1))
							contentintent.putExtra("next", Global.mNews3.get(currentposition+1).GetNo());
						startActivity(contentintent);
						ContentActivity3.this.overridePendingTransition(0, 0);
						
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						finish();
					}
					
					viewpagercurrentpos = position;
					
				}
				
				@Override
				public void onPageScrolled(int arg0, float arg1, int arg2) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onPageScrollStateChanged(int arg0) {
					// TODO Auto-generated method stub
					
					
					
				}
			});
	}

}
